package org.winFame.dispy;

import javafx.application.Platform;
import org.App;
import org.callTools.CallBackApi;
import org.pojo.MenuData;
import org.tools.SdData;
import org.tools.SdSearchData;
import org.winFame.FlowPaneFrameWork;
import org.winFame.HboxTop;

import java.util.UUID;

public class ThreadGetData implements Runnable{
    public static int pageIndex = 1;
    public static int MaxPage = 1;
    public static String className = UUID.randomUUID().toString();
    @Override
    public void run() {
        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                FlowPaneFrameWork.flowPane.getChildren().clear();
            }
        });
        if(!App.keyWod){
            if(pageIndex<=MaxPage && pageIndex > 0){
                SdData.initUrl(pageIndex,className,new CallBackApi(){
                    @Override
                    public void data(MenuData menuData) {
                        super.data(menuData);
                        MaxPage = menuData.getMaxPage();
                        Platform.runLater(new Runnable() {
                            @Override
                            public void run() {
                                if(HboxTop.isAutoDown){
                                    App.downLen++;
                                    Thread thread = new Thread(new Runnable() {
                                        @Override
                                        public void run() {
                                            SdData.downLoad_Win(menuData.getDownLoad(),App.downLoadPath);
                                        }
                                    });
                                    thread.start();
                                }
                                if(HboxTop.isAutoAllDown){
                                    App.downLen++;
                                    Thread thread = new Thread(new Runnable() {
                                        @Override
                                        public void run() {
                                            SdData.downLoad_Win(menuData.getDownLoad(),App.downLoadPath);
                                        }
                                    });
                                    thread.start();
                                }
                                App.stage.setTitle("www.snailcatmall.com - SD模型批量下载工具  数量： "+pageIndex*20+" 下载队列： "+App.downLen);
                                FlowPaneFrameWork.flowPane.getChildren().add(
                                        new PaneBlockframeWork().init(menuData.getImgUrl(),menuData.getNameUrl(),menuData.getDownLoad())
                                );
                            }
                        });
                    }
                });
                if(HboxTop.isAutoAllDown){
                    ThreadGetData.pageIndex++;
                    run();
                }
            }else{
                HboxTop.isAutoAllDown = false;
                HboxTop.isAutoDown = false;
                Platform.runLater(new Runnable() {
                    @Override
                    public void run() {
                        HboxTop.autoDown.setText("自动下载当前页：关");
                        HboxTop.autoAllDown.setText("自动下载所有页数：关");
                    }
                });

                pageIndex=1;
                SdData.initUrl(pageIndex,className,new CallBackApi(){
                    @Override
                    public void data(MenuData menuData) {
                        super.data(menuData);
                        MaxPage = menuData.getMaxPage();
                        Platform.runLater(new Runnable() {
                            @Override
                            public void run() {
                                App.stage.setTitle("www.snailcatmall.com - SD模型批量下载工具  数量： "+pageIndex*20+" 下载队列： "+App.downLen);
                                FlowPaneFrameWork.flowPane.getChildren().add(
                                        new PaneBlockframeWork().init(menuData.getImgUrl(),menuData.getNameUrl(),menuData.getDownLoad())
                                );
                            }
                        });
                    }
                });
            }
        }else {
            if(pageIndex<=MaxPage && pageIndex > 0){
                SdSearchData.initUrl(pageIndex,className, HboxTop.toKeyWord,new CallBackApi(){
                    @Override
                    public void data(MenuData menuData) {
                        MaxPage = menuData.getMaxPage();
                        Platform.runLater(new Runnable() {
                            @Override
                            public void run() {
                                if(HboxTop.isAutoDown){
                                    App.downLen++;
                                    Thread thread = new Thread(new Runnable() {
                                        @Override
                                        public void run() {
                                            SdData.downLoad_Win(menuData.getDownLoad(),App.downLoadPath);
                                        }
                                    });
                                    thread.start();
                                }
                                if(HboxTop.isAutoAllDown){
                                    App.downLen++;
                                    Thread thread = new Thread(new Runnable() {
                                        @Override
                                        public void run() {
                                            SdData.downLoad_Win(menuData.getDownLoad(),App.downLoadPath);
                                        }
                                    });
                                    thread.start();
                                }
                                App.stage.setTitle("www.snailcatmall.com - SD模型批量下载工具  数量： "+pageIndex*20+" 下载队列： "+App.downLen);
                                FlowPaneFrameWork.flowPane.getChildren().add(
                                        new PaneBlockframeWork().init(menuData.getImgUrl(),menuData.getNameUrl(),menuData.getDownLoad())
                                );
                            }
                        });
                    }
                });
                if(HboxTop.isAutoAllDown){
                    ThreadGetData.pageIndex++;
                    run();
                }
            }else{
                System.out.println("asdsadasd");
                HboxTop.isAutoAllDown = false;
                HboxTop.isAutoDown = false;
                Platform.runLater(new Runnable() {
                    @Override
                    public void run() {
                        HboxTop.autoDown.setText("自动下载当前页：关");
                        HboxTop.autoAllDown.setText("自动下载所有页数：关");
                    }
                });
                pageIndex=1;
                SdSearchData.initUrl(pageIndex,className, HboxTop.toKeyWord,new CallBackApi(){
                    @Override
                    public void data(MenuData menuData) {
                        MaxPage = menuData.getMaxPage();
                        Platform.runLater(new Runnable() {
                            @Override
                            public void run() {
                                App.stage.setTitle("www.snailcatmall.com - SD模型批量下载工具  数量： "+pageIndex*20+" 下载队列： "+App.downLen);
                                FlowPaneFrameWork.flowPane.getChildren().add(
                                        new PaneBlockframeWork().init(menuData.getImgUrl(),menuData.getNameUrl(),menuData.getDownLoad())
                                );
                            }
                        });
                    }
                });
            }
        }
//        pageIndex++;
    }
}
